package org.footballmanager.pandafootball.system.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.footballmanager.pandafootball.common.exception.ExceptionEnum;
import org.footballmanager.pandafootball.common.exception.MyException;
import org.footballmanager.pandafootball.system.domain.Photo;
import org.footballmanager.pandafootball.system.domain.enums.FL;
import org.footballmanager.pandafootball.system.service.ImageUpload;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Slf4j
@Service("ImageUploader")
public class ImageUploaderImpl implements ImageUpload {
	
	@CacheEvict(value="showtime",key="'foot'+#photo.type.text")
	public void delete(Photo photo, HttpServletRequest request){
		String filepath = photo.getUrl();
		File file = new File(ClassUtils.getDefaultClassLoader().getResource("").getPath() + filepath);
		if(file.exists()){
			boolean ok = file.delete();
			log.info("Delete file :" + ok);
		}
	}
	
	@CacheEvict(value="showtime",key="'foot'+#fl.text")
	public String addPhoto(MultipartFile myfile, HttpServletRequest request, FL fl){
        //如果只是上传一个文件，则只需要MultipartFile类型接收文件即可，而且无需显式指定@RequestParam注解  
        //如果想上传多个文件，那么这里就要用MultipartFile[]类型来接收文件，并且还要指定@RequestParam注解  
        //并且上传多个文件时，前台表单中的所有<input type="file"/>的name都应该是myfiles，否则参数里的myfiles无法获取到所有上传的文件  
        if(null != myfile && !myfile.isEmpty()){
        	 //如果用的是Tomcat服务器，则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中  
            String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "/images/" + fl.getValue() + "/";
            //这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉，我是看它的源码才知道的
            File path = new File(realPath);
            if(!path.exists()){
                boolean ok = path.mkdirs();
                log.info("Make Dir:" + ok);
            }
            try {
				FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
				log.info(realPath + myfile.getOriginalFilename());
			} catch (IOException e) {
                throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
			}
            return "images/" + fl.getValue() + "/" + myfile.getOriginalFilename();
        }
        return null;
    }  
	
	@CacheEvict(value="showtime",key="'foot'+#fl.text")
	public String upload(MultipartFile myfile, HttpServletRequest request, FL fl) {
        if(null != myfile && !myfile.isEmpty()){
            String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "/images/" + fl.getValue() + "/";
            File file = new File(realPath + myfile.getOriginalFilename());
            if(file.exists()){
                boolean ok = file.delete();
                log.info("Delete file :" + ok);
            }
            File path = new File(realPath);
            if(!path.exists()){
            	boolean ok = path.mkdirs();
                log.info("Make Dir:" + ok);
            }
            try {
				FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
			} catch (IOException e) {
                throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
			}
            return "images/" + fl.getValue() + "/" + myfile.getOriginalFilename();
        }
        return null;
	}

}
